#coding=utf-8

from selenium import webdriver
from appium import webdriver
from time import sleep
from selenium.common.exceptions import WebDriverException
import settings
from nav_plugin_op import nav_plugin_factory
from Public import BasePage
from Tool.usefulTools import userfulToolsFactory
from Public.BasePage import element_Oprate
from Public.BasePage import BasicAction
from Public import BaseConstant
import loginEgovaMobile
from Public import CaseExcelOperate

#实例化部分类
navOpenFactory = nav_plugin_factory()
userfulTool = userfulToolsFactory()
element_oprate_instance = element_Oprate()
basicAction = BasicAction()
Owner_Paramter_instances = BaseConstant.Owner_Paramter
login_system_instances = loginEgovaMobile.login_factory()
excel_operate_instances = CaseExcelOperate.excel_Operate()

nav_name = ["导航栏我的",'xpath',"//android.widget.LinearLayout[@resource-id='cn.com.egova.egovamobile:id/base_tab_tabs_bottom']/android.widget.LinearLayout[4]/android.widget.FrameLayout[1]/android.widget.RadioButton[1]"]
new_tel_keys= "13511112222" #新电话号码
add_phrase_key = "你好！"#惯用语
new_email_keys = "12345@qq.com"#新邮箱地址
psw_old_keys = ""
psw_new_keys = "egova"
psw_new_confirm_keys = "Egova"
save_time_keys = "6"

class Personal_Center():

	def open_owner(self,driver,file_path):
		# 点击我，打开个人中心
		try:
			sleep(5)
			# 点击我的导航，打开个人中心
			element_oprate_instance.element_click(nav_name,file_path)
			element_oprate_instance.element_save_pic(file_path, 'open_owner')

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'open_owner_fail')
			raise

	def open_personal_information(self,driver,file_path):
		# 个人信息设置
		try:
			#点击用户名，进入个人信息
			sleep(3)
			element_oprate_instance.element_click(Owner_Paramter_instances.user_name_id, file_path)
			# 点击用户头像
			# driver.find_element_by_id(user_picture_id).click()#???????????未完成
			# 点击手机号码，输入新号码
			element_oprate_instance.element_click(Owner_Paramter_instances.user_tel_id, file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.new_tel_id, file_path)
			element_oprate_instance.element_send_keys(Owner_Paramter_instances.new_tel_id,new_tel_keys.decode('utf-8'),  file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.ok_id, file_path)
			#点击邮箱，输入新邮箱
			element_oprate_instance.element_click(Owner_Paramter_instances.user_email_id, file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.new_email_id, file_path)
			element_oprate_instance.element_send_keys(Owner_Paramter_instances.new_email_id, new_email_keys.decode('utf-8'),file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.ok_id, file_path)

			#点击性别，设置性别
			element_oprate_instance.element_click(Owner_Paramter_instances.user_gender_id, file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.new_gender_keys, file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.ok_id, file_path)
			# 截图保存上述设置
			element_oprate_instance.element_save_pic(file_path, 'tel_email_gender_set')
			#进入我的收藏
			element_oprate_instance.element_click(Owner_Paramter_instances.user_collect_id, file_path)
			element_oprate_instance.element_save_pic(file_path, 'personal_collection')
			element_oprate_instance.element_click(Owner_Paramter_instances.user_collect_exit_name, file_path)
			#点击修改密码
			element_oprate_instance.element_click(Owner_Paramter_instances.user_password_id, file_path)
			element_oprate_instance.element_send_keys(Owner_Paramter_instances.psw_old_id,psw_old_keys.decode('utf-8'), file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.psw_new_id, file_path)
			element_oprate_instance.element_send_keys(Owner_Paramter_instances.psw_new_id, psw_new_keys.decode('utf-8'),file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.psw_new_confirm_id, file_path)
			element_oprate_instance.element_send_keys(Owner_Paramter_instances.psw_new_confirm_id,psw_new_confirm_keys.decode("utf-8"), file_path)
			element_oprate_instance.element_save_pic(file_path, 'personal_psw_set')
			element_oprate_instance.element_click(Owner_Paramter_instances.cance_id, file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.exit_personal_information, file_path)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'personal_information_set_fail')
			raise

	def speed_dial(self,driver,file_path):
		#进入单键拨号
		try:
			element_oprate_instance.element_click(Owner_Paramter_instances.speed_dial_id, file_path)
			#输入号码
			element_oprate_instance.element_send_keys(Owner_Paramter_instances.dial_number_id,new_tel_keys.decode("utf-8"), file_path)
			element_oprate_instance.element_save_pic(file_path, 'dial_number_set')
			#退出单键拨号
			element_oprate_instance.element_click(Owner_Paramter_instances.exit_name, file_path)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'dial_number_set_fail')
			raise

	def function_manage(self,driver,file_path):	
		#点击功能管理
		try:
			element_oprate_instance.element_click(Owner_Paramter_instances.function_manage_id, file_path)
			#点击全部安装
			element_oprate_instance.element_click(Owner_Paramter_instances.plugin_extend_name, file_path)
			element_oprate_instance.element_save_pic(file_path, 'plugin_extend')
			#退出功能管理
			element_oprate_instance.element_click(Owner_Paramter_instances.dial_number_exit_name, file_path)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'plugin_extend_fail')
			raise

	def function_setting(self,driver,file_path):
		#点击功能设置
		try:
			element_oprate_instance.element_click(Owner_Paramter_instances.function_setting_id, file_path)
			#点击案卷设置提醒
			sleep(2)
			# driver.find_element_by_id(Owner_Paramter_instances.case_reminder_setting).click()
			element_oprate_instance.element_click(Owner_Paramter_instances.case_reminder_setting, file_path)
			#设置提醒时间
			element_oprate_instance.element_send_keys(Owner_Paramter_instances.config_time_id,'6', file_path)
			#设置提醒方式
			element_oprate_instance.element_click(Owner_Paramter_instances.congif_way_id, file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.congif_way_keys, file_path)
			element_oprate_instance.element_save_pic(file_path ,"//case_reminder_setting.png")
			#点击确定
			element_oprate_instance.element_click(Owner_Paramter_instances.ok_id,file_path)
			
			#点击上报历史记录设置
			# element_oprate_instance.element_click(Owner_Paramter_instances.function_setting_id, file_path)
			# driver.find_element_by_id(Owner_Paramter_instances.report_history_id).click()
			# #设置保留时间
			# element_oprate_instance.element_click(Owner_Paramter_instances.history_num_id, file_path)
			# element_oprate_instance.elemet_clear(Owner_Paramter_instances.history_num_id, file_path)
			# element_oprate_instance.element_send_keys(Owner_Paramter_instances.history_num_id,save_time_keys.decode("utf-8"), file_path)
			# element_oprate_instance.element_save_pic(file_path, 'case_savetime_setting')
			# #点击确定
			# sleep(2)
			# element_oprate_instance.element_click(Owner_Paramter_instances.confirm_name, file_path)

			#点击惯用语设置
			element_oprate_instance.element_click(Owner_Paramter_instances.phrase_setting_name, file_path)
			#输入惯用语
			element_oprate_instance.element_send_keys(Owner_Paramter_instances.add_phrase_id,add_phrase_key.decode("utf-8"), file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.add_phrase_bt_id, file_path)
			element_oprate_instance.element_save_pic(file_path, 'phrase_setting')
			#返回
			element_oprate_instance.element_click(Owner_Paramter_instances.phrase_setting_name, file_path)
			element_oprate_instance.element_click(Owner_Paramter_instances.up_name, file_path)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'function_setting_fail')
			raise

	# def system_support(self,driver,file_path):
	# 	#点击系统帮助
	# 	try:

	def traffic_statistics(self,driver,file_path):
		#点击流量统计
		try:
			element_oprate_instance.element_click(Owner_Paramter_instances.traffic_statistics_id, file_path)
			sleep(3)
			element_oprate_instance.element_save_pic(file_path, 'traffic_statistics')
			element_oprate_instance.element_click(Owner_Paramter_instances.Exit_name, file_path)
			
		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'traffic_statistics_fail')
			raise		

	def clear_cache(self,driver,file_path):
		#点击清除缓存
		try:
			element_oprate_instance.element_click(Owner_Paramter_instances.clear_cache_id, file_path)
			#勾选搜索记录
			element_oprate_instance.element_click(Owner_Paramter_instances.clear_item_name, file_path)
			element_oprate_instance.element_save_pic(file_path, 'clear_cache')
			#点击确定
			element_oprate_instance.element_click(Owner_Paramter_instances.ok_id, file_path)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'clear_cache_fail')
			raise

	def drop_out(self,driver,file_path):
		#点击退出
		try:
			driver.swipe(400,250,400,50,800)
			element_oprate_instance.element_click(Owner_Paramter_instances.drop_out_id, file_path)
			#点击确定
			element_oprate_instance.element_click(Owner_Paramter_instances.ok_id, file_path)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'exit_fail')
			raise

	#重新登录--使用新密码
	def re_login_newPassword(self):
		user_infor = excel_operate_instances.get_userInfo(settings.test_case_file, 'owner_test')
		login_system_instances.login_system(BasePage.Base.driver,user_infor[0],psw_new_keys,settings.pic_file_path)
		element_oprate_instance.element_save_pic(settings.pic_file_path,'re_login_successful')

		
if __name__ == '__main__':

    print '0'
	# suite = unittest.TestSuite()
	# # 添加测试用例
	# suite.addTest(Personal_Center('open_owner'))
	# suite.addTest(Personal_Center('traffic_statistics'))






	
			
